/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author johnkim
 *
 */
public class DayveCategoryDatabaseHandler {

	public static final String TAG = DayveCategoryDatabaseHandler.class.getSimpleName();
	
	Context ctx;
	DayveCategoryDatabaseHelper helper;
	SQLiteDatabase db;
	
	public DayveCategoryDatabaseHandler(Context ctx) {
		this.ctx = ctx;
		
		helper = new DayveCategoryDatabaseHelper(ctx);		
		
		db = helper.getWritableDatabase();
	}
	
	/**
	 * Database open 
	 */	
    public static DayveCategoryDatabaseHandler open(Context ctx) {
    	return new DayveCategoryDatabaseHandler(ctx);
    }
    
    /**
     * Transaction 을 정의 한다.
     */
	public void beginTransaction() {db.beginTransaction();}
	
	public void setTransactionSuccessful() {db.setTransactionSuccessful();}
	
	public void endTransaction() {db.endTransaction();}

	public SQLiteDatabase getSQLiteDatabase() {
		return this.db;
	}
	
	/**
	 * Database close 
	 * */
	public void close() {
		if(db.isOpen()) db.close();
	}

	// Category 테이블 컬럼 목록.
	private static final String[] DAYVE_CATEGORY = new String[] {
		"_id", 
		"id_myhistory_categories",
		"name",
		"comment", 
		"icon_url", 
		"open_category_yn",
		"favorite_yn",
		"activate_yn",
		"modify_date",
		"register_date",
		"myhistory_members_id", 
		"items_count" 
	};
	
	/**
	 * Category 내용을 가져 온다.
	 * 
	 * @param idMyHistoryCategories
	 * @return
	 */
	public DayveCategory selectCategory(String idMyHistoryCategories) {
		Log.i(TAG, "selectCategory");
		
		Log.i(TAG, "idMyHistoryCategories : " + idMyHistoryCategories);
		
		DayveCategory dayveCategory = new DayveCategory();
		
		Cursor cursor = db.query(
				"dayve_category", 							// 테이블.
				DAYVE_CATEGORY, 							// 컬럼.
				"id_myhistory_categories = ?", 				// 조건문.
				new String[] {idMyHistoryCategories}, 		// 조건값.
				null, 
				null, 
				null
				);
		
		/**
		 * 현재, 페이징 처리와 한번에 가져 오는 로직이 섞여 있어서 
		 * 같은 데이터들이 중복으로 들어 있기때문에 같은 데이터의 첫번째 결과만 
		 * 가져 오도록 cursor.moveToFirst() 정의 함.
		 * 
		 * 추후 한번에 가져 오는 로직으로 모두 수정될 경우 삭제 하면 된다.
		 */
		cursor.moveToFirst();
		
		Log.i(TAG, "cursor.getCount() : " + cursor.getCount());
		
		dayveCategory.set_id(cursor.getString(0));
		dayveCategory.setIdMyHistoryCategories(cursor.getString(1));
		dayveCategory.setName(cursor.getString(2));
		dayveCategory.setComment(cursor.getString(3));
		dayveCategory.setIconUrl(cursor.getString(4));
		dayveCategory.setOpenCategoryYn(cursor.getString(5));
		dayveCategory.setFavoriteYn(cursor.getString(6));
		dayveCategory.setActivateYn(cursor.getString(7));
		dayveCategory.setModifyDate(cursor.getString(8));
		dayveCategory.setRegisterDate(cursor.getString(9));
		dayveCategory.setMyHistoryMembersId(cursor.getString(10));
		dayveCategory.setItemsCount(cursor.getString(11));

		cursor.close();
		
		return dayveCategory;
	}
	
	/**
	 * 카테고리 목록을 가져 온다.
	 * 
	 * @param _id
	 * @param limit
	 * @return
	 */
	public ArrayList<DayveCategory> selectCategoryList(String _id, String limit) {
		Log.i(TAG, "selectCategoryList");
		
		ArrayList<DayveCategory> categoryList = new ArrayList<DayveCategory>();
		
		Cursor cursor = db.query(
				"dayve_category", 
				DAYVE_CATEGORY, 
				"_id>?", 
				new String[] {_id}, 
				null, 
				null, 
				" _id ASC ", 
				limit
				);
		
		Log.i(TAG, "_id   : " + _id);
		Log.i(TAG, "limit : " + limit);
		
		DayveCategory dayveCategory = null;
		
		while(cursor.moveToNext()) {
			dayveCategory = new DayveCategory();
			
			dayveCategory.set_id(cursor.getString(0));
			dayveCategory.setIdMyHistoryCategories(cursor.getString(1));
			dayveCategory.setName(cursor.getString(2));
			dayveCategory.setComment(cursor.getString(3));
			dayveCategory.setIconUrl(cursor.getString(4));
			dayveCategory.setOpenCategoryYn(cursor.getString(5));
			dayveCategory.setFavoriteYn(cursor.getString(6));
			dayveCategory.setActivateYn(cursor.getString(7));
			dayveCategory.setModifyDate(cursor.getString(8));
			dayveCategory.setRegisterDate(cursor.getString(9));
			dayveCategory.setMyHistoryMembersId(cursor.getString(10));
			dayveCategory.setItemsCount(cursor.getString(11));
			
			categoryList.add(dayveCategory);
		}
		
		cursor.close();
		
		return categoryList;
	}
	
	/**
	 * Group 의 Category 목록을 가져 온다.
	 * 
	 * @param groupId
	 * @return
	 */
	public ArrayList<DayveCategory> selectCategoryListForGroup(String groupId) {
		Log.i(TAG, "selectCategoryListForGroup");
		
		ArrayList<DayveCategory> categoryList = new ArrayList<DayveCategory>();
		
		Cursor cursor = db.query(
				"dayve_category", 
				DAYVE_CATEGORY, 
				"myhistory_groups_id_myhistory_groups=?", 
				new String[] { groupId }, 
				null, 
				null, 
				" _id ASC "
				);
		
		DayveCategory dayveCategory = null;
		
		while(cursor.moveToNext()) {
			dayveCategory = new DayveCategory();
			
			dayveCategory.set_id(cursor.getString(0));
			dayveCategory.setIdMyHistoryCategories(cursor.getString(1));
			dayveCategory.setName(cursor.getString(2));
			dayveCategory.setComment(cursor.getString(3));
			dayveCategory.setIconUrl(cursor.getString(4));
			dayveCategory.setOpenCategoryYn(cursor.getString(5));
			dayveCategory.setFavoriteYn(cursor.getString(6));
			dayveCategory.setActivateYn(cursor.getString(7));
			dayveCategory.setModifyDate(cursor.getString(8));
			dayveCategory.setRegisterDate(cursor.getString(9));
			dayveCategory.setMyHistoryMembersId(cursor.getString(10));
			dayveCategory.setItemsCount(cursor.getString(11));
			
			categoryList.add(dayveCategory);
		}
		
		cursor.close();
		
		return categoryList;
	}
	
	/**
	 * 즐겨 찾기 Category 목록을 가져 온다.
	 * 
	 * @param _id
	 * @param limit
	 * @return
	 */
	public ArrayList<DayveCategory> selectFavoriteCategoryList(String _id, String limit) {
		Log.i(TAG, "selectCategoryList");
		
		ArrayList<DayveCategory> categoryList = new ArrayList<DayveCategory>();
		
		Cursor cursor = db.query(
				"dayve_category", 
				DAYVE_CATEGORY, 
				"_id>? and favorite_yn=?", 
				new String[] {_id, "Y"}, 
				null, 
				null, 
				" _id ASC ", 
				limit
				);
		
		Log.i(TAG, "_id   : " + _id);
		Log.i(TAG, "limit : " + limit);
		Log.i(TAG, "query : " + cursor.toString());
		
		
		DayveCategory dayveCategory = null;
		
		while(cursor.moveToNext()) {
			dayveCategory = new DayveCategory();
			
			dayveCategory.set_id(cursor.getString(0));
			dayveCategory.setIdMyHistoryCategories(cursor.getString(1));
			dayveCategory.setName(cursor.getString(2));
			dayveCategory.setComment(cursor.getString(3));
			dayveCategory.setIconUrl(cursor.getString(4));
			dayveCategory.setOpenCategoryYn(cursor.getString(5));
			dayveCategory.setFavoriteYn(cursor.getString(6));
			dayveCategory.setActivateYn(cursor.getString(7));
			dayveCategory.setModifyDate(cursor.getString(8));
			dayveCategory.setRegisterDate(cursor.getString(9));
			dayveCategory.setMyHistoryMembersId(cursor.getString(10));
			dayveCategory.setItemsCount(cursor.getString(11));
			
			categoryList.add(dayveCategory);
		}
		
		cursor.close();
		
		return categoryList;
	}
	
	/**
	 * 카테고리를 저장 한다.
	 * 
	 * @param values
	 * @return
	 */
	public Long insertDayveCategory(ContentValues values) {
//		Log.i(TAG, "insertDayveCategory");
		return db.insert("dayve_category", "", values);
	}
	
	/**
	 * 카테고리를 삭제 한다.
	 * 
	 * @return
	 */
	public int deleteDayveCategory() {
		Log.i(TAG, "deleteDayveCategory");
		return db.delete("dayve_category", null, null);
	}

}
